﻿package frEngine.effectEditTool.manager
{
    import __AS3__.vec.*;
    import baseEngine.basic.*;
    import baseEngine.core.*;
    import baseEngine.modifiers.*;
    import com.gengine.gc.*;
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import flash.utils.*;
    import frEngine.core.mesh.*;
    import frEngine.loaders.*;
    import frEngine.loaders.away3dMd5.*;
    import frEngine.loaders.away3dMd5.md5Data.*;
    import frEngine.pools.*;

    public class MeshLoaderManager extends Object
    {

        public function MeshLoaderManager()
        {
            return;
        }// end function

        public static function isMd5Mesh(param1:String) : Boolean
        {
            var _loc_2:* = param1.toUpperCase();
            if (_loc_2.indexOf(".MD5MESH") != -1)
            {
                return true;
            }
            return false;
        }// end function

        public static function getMesh3dByUrl(param1:Object, param2:String, param3, param4:String, param5:String, param6:Boolean, param7:uint, param8:RenderList) : Mesh3D
        {
            var _loc_10:Mesh3D = null;
            var _loc_11:Md5Mesh = null;
            var _loc_12:NormalMesh3D = null;
            var _loc_9:* = isMd5Mesh(param2);
            if (_loc_9)
            {
                _loc_11 = FrObjectPool.getObject(Md5Mesh, param1, "");
                _loc_11.reinit(param2, param8);
                _loc_10 = _loc_11;
                if (param3)
                {
                    _loc_11.setMaterial(param3, Texture3D.MIP_NONE, param3);
                }
                _loc_11.initAnimate(param4, param5);
                _loc_11.targetMd5Controler.offsetPlayFrame = param7;
            }
            else
            {
                _loc_12 = FrObjectPool.getObject(NormalMesh3D, param1, "");
                _loc_12.reinit(param2, param8);
                _loc_10 = _loc_12;
                if (param3)
                {
                    _loc_10.setMaterial(param3, Texture3D.MIP_NONE, param3);
                }
            }
            _loc_10.animationMode = param6 ? (PlayMode.ANIMATION_LOOP_MODE) : (PlayMode.ANIMATION_STOP_MODE);
            return _loc_10;
        }// end function

        public static function getBonesTree(param1:String) : SkeletonJoint
        {
            if (param1 == null || param1 == EngineConstName.defaultNullStringFlag)
            {
                return null;
            }
            var _loc_2:* = getFileBytes2(param1);
            if (!MeshLoaderManager.isMd5Mesh(param1))
            {
                return null;
            }
            var _loc_3:* = MD5MeshByteArrayParser.parseHeadAndJoints(_loc_2, null);
            if (!_loc_3)
            {
                return null;
            }
            return _loc_3.joinRoot;
        }// end function

        public static function checkMeshAndBoneIsMatch(param1:String, param2:String) : Vector.<SkeletonClipNode>
        {
            if (!param1 || param1.length == 0 || param1 == EngineConstName.defaultNullStringFlag || !param2 || param2.length == 0 || param2 == EngineConstName.defaultNullStringFlag)
            {
                return null;
            }
            var _loc_3:* = getFileBytes2(param1);
            var _loc_4:* = getFileBytes2(param2);
            if (!_loc_3 || !_loc_4)
            {
                return null;
            }
            var _loc_5:* = MD5MeshByteArrayParser.parseHeadAndJoints(_loc_3, null);
            if (!_loc_5)
            {
                return null;
            }
            var _loc_6:* = MD5AnimByteArrayParser.parseHeadAndJoints(_loc_4);
            if (!_loc_6)
            {
                return null;
            }
            var _loc_7:* = _loc_5.inherits;
            var _loc_8:* = _loc_6.joinsList;
            var _loc_9:* = Math.min(_loc_7.length, _loc_8.length);
            var _loc_10:int = 0;
            while (_loc_10 < _loc_9)
            {
                
                if (_loc_7[_loc_10].name != _loc_8[_loc_10].name)
                {
                    return null;
                }
                _loc_10++;
            }
            return _loc_6.tracksList;
        }// end function

        public static function getFileBytes2(param1:String) : ByteArray
        {
            var _loc_2:* = ResourceManager.getInfoByName(param1);
            if (!_loc_2)
            {
                return null;
            }
            var _loc_3:* = getDefinitionByName("flash.filesystem.File") as Class;
            var _loc_4:* = new _loc_3(_loc_2.soPath);
            var _loc_5:* = getDefinitionByName("flash.filesystem.FileStream") as Class;
            var _loc_6:* = new _loc_5;
            _loc_6.open(_loc_4, "read");
            var _loc_7:* = new GByteArray();
            _loc_7.endian = Endian.LITTLE_ENDIAN;
            _loc_6.readBytes(_loc_7);
            return _loc_7;
        }// end function

    }
}
